package com.pucheng.bracelet.ui.activity.blood_pressure

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.App
import com.pucheng.bracelet.app.base.BaseHistoryToListActivity
import com.pucheng.bracelet.app.database.repository.BPRepository
import com.pucheng.bracelet.data.bean.BloodPressureBean
import com.pucheng.bracelet.data.bean.Sao2Bean
import com.pucheng.bracelet.viewmodel.blood_pressure.BPModelFactory
import com.pucheng.bracelet.viewmodel.blood_pressure.BloodPressureViewModel
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

/**
 * 血压记录界面
 */
class BPHistoryActivity : BaseHistoryToListActivity<BaseViewModel, BloodPressureBean>() {

    private var historyLists = mutableListOf<BloodPressureBean>()
    private lateinit var mAdapter: BaseQuickAdapter<BloodPressureBean, BaseViewHolder>
    private val bpViewModel: BloodPressureViewModel by viewModels { BPModelFactory(BPRepository((application as App).database.bloodPressureDao())) }

    override fun titleResId(): Int = R.string.blood_pressure_history

    override fun initView(savedInstanceState: Bundle?) {

        mAdapter = object : BaseQuickAdapter<BloodPressureBean, BaseViewHolder>(
            R.layout.item_list_history,
            historyLists
        ) {
            override fun convert(holder: BaseViewHolder, item: BloodPressureBean) {
                holder.setTextColorRes(R.id.tv_history_content, R.color.blood_pressure_bg).setText(
                    R.id.tv_history_content,
                    item.name + getString(R.string.blood_pressure_unit)
                )
                    .setText(R.id.tv_history_time, item.measuredDate)
            }
        }
        super.initView(savedInstanceState)

    }

    override fun getHistoryAdapter(): BaseQuickAdapter<BloodPressureBean, BaseViewHolder> = mAdapter


    override fun createObserver() {
        super.createObserver()
        bpViewModel.apply {
            allData.observe(this@BPHistoryActivity, Observer {
                it.let {
                    historyLists.clear()
                    historyLists.addAll(it as MutableList<BloodPressureBean>)
                    mAdapter.notifyDataSetChanged()
                }
            })
        }
    }
}